package servlets;

import hbt.dao.HibernateAlumnoDAO;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entities.Alumno;
import entities.PersonaId;
@SuppressWarnings("unchecked")
public class BuscarAlumnoServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		String opcion = req.getParameter("opcion");

		if(opcion.equalsIgnoreCase("volver")){
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/jsp/index.jsp");
			rd.forward(req, resp);
		}	
		else if(opcion.equalsIgnoreCase("buscar")){
			String numeroDoc = req.getParameter("numeroDoc");
			String nombre = req.getParameter("nombre");
			List<Alumno> alumnos = HibernateAlumnoDAO.getInstancia().buscarAlumno(numeroDoc,nombre);
			req.setAttribute("ALUMNOS", alumnos);
			req.getSession().setAttribute("ALUMNOS", alumnos);
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/jsp/buscarAlumnos.jsp");
			rd.forward(req, resp);
		}else if(opcion.equalsIgnoreCase("display")){
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/jsp/buscarAlumnos.jsp");
			rd.forward(req, resp);
		}else if(opcion.equalsIgnoreCase("aceptar")){
			List<Alumno> alumnos = (List<Alumno>) req.getSession().getAttribute("ALUMNOS");
			req.getSession().setAttribute("ALUMNOS", alumnos);
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/jsp/agregarMateria.jsp");
			rd.forward(req, resp);
		}
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String opcion = req.getParameter("opcion");

		if(opcion.equalsIgnoreCase("volver")){
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/jsp/index.jsp");
			rd.forward(req, resp);
		}	
		else if(opcion.equalsIgnoreCase("buscar")){
			String numeroDoc = req.getParameter("numeroDoc");
			String nombre = req.getParameter("nombre");
			List<Alumno> alumnos = HibernateAlumnoDAO.getInstancia().buscarAlumno(numeroDoc,nombre);
			req.setAttribute("ALUMNOS", alumnos);
			req.getSession().setAttribute("ALUMNOS", alumnos);
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/jsp/buscarAlumnos.jsp");
			rd.forward(req, resp);
		}else if(opcion.equalsIgnoreCase("display")){
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/jsp/buscarAlumnos.jsp");
			rd.forward(req, resp);
		}else if(opcion.equalsIgnoreCase("aceptar")){
			Alumno alumno = new Alumno();
			PersonaId personaId = new PersonaId();
			personaId.setTipo(req.getParameter("tipoAlumno"));
			alumno.setAnioCursado(Integer.parseInt(req.getParameter("anioAlumno")));
			alumno.setNombre(req.getParameter("nombreAlumno"));
			personaId.setNumero(Integer.parseInt(req.getParameter("numeroAlumno")));
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/jsp/agregarMateria.jsp");
			rd.forward(req, resp);
		}
	}

}
